package com.tongji.controller;

import com.bean.PanDian;
import com.bean.WmsTCrDetails;
import com.bean.WmsTCustomer;
import com.bean.WmsTStore;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tongji.service.PandianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("pandian")
public class PandianController {

    @Autowired
    PandianService pandianService;

    //跳转到盘点页面 & 查询有东西的仓库
    @RequestMapping("toPandian.do")
    public String toPandian(HttpServletRequest request) {

        System.out.println("到达跳转库存盘点请求");

        //查询仓库
        List<WmsTStore> wmsTStores = pandianService.getStore();
        //放入session域
        request.getSession().setAttribute("wmsTStores", wmsTStores);
        System.out.println("有东西的仓库为：" + wmsTStores);


        //开始分页
        PageHelper.startPage(1, 1);
        //查询数据
        List<PanDian> panDians = pandianService.getPanDian("李四", "01");
        System.out.println("盘点的内容：" + panDians);
        //分页显示
        PageInfo<PanDian> pageInfo = new PageInfo<PanDian>(panDians);
        System.out.println("分页为：");
        System.out.println(pageInfo);
        //放域
        request.getSession().setAttribute("pageInfopandian", pageInfo);


        return "pandian/kucunpandian";
    }


    //keyup时间的客户信息查询
    @RequestMapping("getCustomerName.do")
    @ResponseBody
    public List<WmsTCustomer> getCustomerName(String name) {

        System.out.println("前端传过来的name为：" + name);
        List<WmsTCustomer> wmsTCustomers = pandianService.getCustomer(name);
        System.out.println("模糊查询的集合为：" + wmsTCustomers);

        return wmsTCustomers;
    }


    //开始盘点
    @RequestMapping("StartCount.do")
    public String StartCount(String name, String store, int pageNum, int pageSize, HttpServletRequest request) {

        //货主和仓库放域
        request.getSession().setAttribute("name", name);
        request.getSession().setAttribute("store", store);

        name = name.substring(name.lastIndexOf(":") + 1);
        System.out.println("货主姓名：" + name + "仓库ID：" + store);
        System.out.println("从第" + pageNum + "页开始，每页" + pageSize + "条");

        //pageSize & pageNum 放域
        request.getSession().setAttribute("num", pageNum);
        request.getSession().setAttribute("size", pageSize);


        //往WMS_T_CR_DETAILS插入一条记录，将具体的details的信息插入该表

        //查询数据
        List<PanDian> panDians2 = pandianService.getPanDian(name, store);
        //循环插入
        for (int i = 0; i < panDians2.size(); i++) {

            //UUID生成随机id
            String detailsID = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
            WmsTCrDetails wmsTCrDetails = new WmsTCrDetails();
            wmsTCrDetails.setDetailsID(detailsID);
            wmsTCrDetails.setChkrptID(panDians2.get(i).getDetailsID());
            wmsTCrDetails.setDetailsGoodsName(panDians2.get(i).getDetailsGoodsName());
            wmsTCrDetails.setDetailsSpec(panDians2.get(i).getDetailsGoodsSpec());
            //获取当前系统时间
            String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            wmsTCrDetails.setDetailsEditTime(date);

            //查询表内是否有该条数据[存在不重复插入]
            if (!(pandianService.getWmsTCrDetails(wmsTCrDetails).size() > 0)) {

                //插入表
                pandianService.insertWmsTCrDetails(wmsTCrDetails);
            }
        }


        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //查询数据
        List<PanDian> panDians = pandianService.getPanDian(name, store);
        System.out.println("盘点的内容：" + panDians);
        //分页显示
        PageInfo<PanDian> pageInfo = new PageInfo<PanDian>(panDians);
        System.out.println("分页为：");
        System.out.println(pageInfo);

        //放域
        request.getSession().setAttribute("pageInfopandian", pageInfo);
        return "pandian/kucunpandian";
    }

    //上下页
    @RequestMapping("TurnPages.do")
    public String TurnPages(String name, String store, int pageNum, int pageSize, HttpServletRequest request) {

        //货主和仓库放域
        request.getSession().setAttribute("name", name);
        request.getSession().setAttribute("store", store);

        name = name.substring(name.lastIndexOf(":") + 1);
        System.out.println("货主姓名：" + name + "仓库ID：" + store);
        System.out.println("从第" + pageNum + "页开始，每页" + pageSize + "条");

        //pageSize & pageNum 放域
        request.getSession().setAttribute("num", pageNum);
        request.getSession().setAttribute("size", pageSize);


        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //查询数据
        List<PanDian> panDians = pandianService.getPanDian(name, store);
        System.out.println("盘点的内容：" + panDians);
        //分页显示
        PageInfo<PanDian> pageInfo = new PageInfo<PanDian>(panDians);
        System.out.println("分页为：");
        System.out.println(pageInfo);

        //放域
        request.getSession().setAttribute("pageInfopandian", pageInfo);
        return "pandian/kucunpandian";
    }
}
